/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package transactions;

import item.Item;
import java.sql.Date;
import java.util.Calendar;
import javax.persistence.*;
import myJPA.Model;
import user.Customer;
import user.Employee;

/**
 *
 * @author Nacer Khalil
 */
@Entity
@Table(name="reservation")
@NamedQueries({
@NamedQuery(name="findReservationNotOnHold",query="select r from Reservation r where item=:item AND onHold='false' AND r.id <= ALL(select s.id from Reservation s)"),
@NamedQuery(name="findNotOnHoldReservations",query="select r from Reservation r where onHold = 'false' and item=:item"),
@NamedQuery(name="findOnHoldReservations",query="select r from Reservation r where onHold = 'true' and item=:item"),
})

public class Reservation extends Model{

    @Id
    @GeneratedValue
    private int id;
    
    @ManyToOne
    @JoinColumn(name = "cus_id")
    private Customer customer; 
    
    @ManyToOne
    @JoinColumn(name = "item_id")
    private Item item; 
    
    @ManyToOne
    @JoinColumn(name = "emp_id")
    private Employee employee;
    
    private Date reservationDate;
    private boolean onHold;
    private Date onHoldDate;

    public Reservation() {
        super();
    }

    public Reservation(Customer customer, Item item, Employee employee) {
        super();
        this.customer = customer;
        this.item = item;
        this.employee = employee;
        this.reservationDate = new Date(java.util.Calendar.getInstance().getTimeInMillis()); //TODAY
        this.onHold = false;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /**
     * @return the item
     */
    public Item getItem() {
        return item;
    }

    /**
     * @param item the item to set
     */
    public void setItem(Item item) {
        this.item = item;
    }

    /**
     * @return the employee
     */
    public Employee getEmployee() {
        return employee;
    }

    /**
     * @param employee the employee to set
     */
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    /**
     * @return the reservationDate
     */
    public Date getReservationDate() {
        return reservationDate;
    }

    /**
     * @return the onHold
     */
    public boolean isOnHold() {
        return onHold;
    }

    /**
     * @param onHold the onHold to set
     */
    public void setOnHold(boolean onHold) {
        if(onHold)
        {
            this.onHoldDate = new Date(java.util.Calendar.getInstance().getTimeInMillis()); //TODAY
        }
        else
        {
            this.onHoldDate = null;
        }
        this.onHold = onHold;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Reservation other = (Reservation) obj;
        if (this.id != other.id) {
            return false;
        }
        if (this.customer != other.customer && (this.customer == null || !this.customer.equals(other.customer))) {
            return false;
        }
        if (this.item != other.item && (this.item == null || !this.item.equals(other.item))) {
            return false;
        }
        if (this.employee != other.employee && (this.employee == null || !this.employee.equals(other.employee))) {
            return false;
        }
        if (this.reservationDate != other.reservationDate && (this.reservationDate == null || !this.reservationDate.equals(other.reservationDate))) {
            return false;
        }
        if (this.onHold != other.onHold) {
            return false;
        }
        if (this.onHoldDate != other.onHoldDate && (this.onHoldDate == null || !this.onHoldDate.equals(other.onHoldDate))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        return hash;
    }
    
     
    
}
